package org.asdmp.restday.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.asdmp.restday.service.RestDayService;
import org.jbpm.api.ProcessInstance;

/**
 * Servlet implementation class createInstanceController
 */
@WebServlet("/controller/createInstanceController")
public class CreateInstanceController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateInstanceController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	} 

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String assignee = request.getParameter("to");
		PrintWriter out = response.getWriter();
		RestDayService.startUp();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("to", assignee);
		ProcessInstance processInstance = RestDayService.executionService.startProcessInstanceByKey("restday", map);		
		RestDayService.processInstanceId = processInstance.getId();
		request.setAttribute("responseText","流程实例ID========"+RestDayService.processInstanceId.toString());
		String name = RestDayService.executionService.createProcessInstanceQuery().processInstanceId(RestDayService.processInstanceId).uniqueResult().findActiveActivityNames().toString();
		request.setAttribute("node",name);
		request.getRequestDispatcher("../jsp/console.jsp").forward(request, response); 
	}

}
